function [dataFD]=FCForceDist(data,k,c)
% Zeros force curves, finds c (constant compliance) values.
%
% INPUT:
%           data - structure with .app and .ret, each containing a cell 
% array of delta_z (um) v. deflection_nA (nA).
%           k - spring constant (N/m)
%           c - slope of constant compliance region (nm/nA)


z = data(:,1).*1000;        %Gives z piezo height in nm
z = z(end:-1:1);
dnm = data(:,2).*c;    %Gives deflection in nm

if length(z)~=length(dnm)         %Sometimes, weirdly, different number of points in z v. dnm data. This corrects the issue.
    z = z(1:length(dnm));
end

%See Passeri Berezina 2009 for a good walk-through of this process

% Correct for contribution of tip deflection, then translate to um.
H = (z- dnm)./1000;     

%Translate dnm into force (nN)
F = dnm.*k;






% max to min def data
% n=931;
% H=H(1:n);
% F=F(1:n);
% 
% temph = H(n);
% tempF = F(n);
% 
% H = H + -temph;
% F = F - tempF;



%min to max def data
% n=819;
% H=H(n:end);
% F=F(n:end);
% 
% temph = H(1);
% tempF = F(1);
% 
% H = H + -temph;
% F = F - tempF;





dataFD = [H F];
% figure; 
plot(H.*1000,F)
% hold on;
% plot(z,F)
% axis([0 60 0 2000]);
